<?php 


header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Transfer-Encoding:utf-8");
header("Pragma: no-cache");
header('Content-Type: application/vnd.ms-excel');
header('Cache-Control: max-age=0');
header("Content-Disposition:filename="."_".date("Ymd").time().".xls");
echo @iconv("UTF-8","Big-5//ignore","序號\t客戶姓名\t推薦人\t重要性\t國籍\t英文姓名\t性別\t客戶來源\t生日\t送禮\t身份證字號\t平常聯絡\t開始時間\t結束時間\t平常聯絡1\t開始時間1\t結束時間1\t平常聯絡2\t開始時間2\t結束時間2\t假日聯絡\t結束時間\t開始時間\t假日聯絡1\t開始時間1\t結束時間1\t假日聯絡2\t開始時間2\t結束時間2\t背景與特色\t行動電話\t簡訊發送\t行動電話1\t簡訊發送1\t行動電話2\t簡訊發送2\t住家電話\t住家電話1\t住家電話2\t公司電話\t公司電話1\t公司電話2\t分機\t分機1\t分機2\t電子郵件\t寄發EDM\t電子郵件1\t寄發EDM1\t電子郵件2\t寄發EDM2\t傳真\t傳真1\t傳真2\t住家地址\t大樓名稱\t住家寄發刊物\t住家地址1\t大樓名稱1\t住家寄發刊物1\t住家地址2\t大樓名稱2\t住家寄發刊物2\t公司名稱\t公司類型\t統一編號\t部門\t職稱\t類別\t公司地址\t大樓名稱\t公司寄發刊物\t公司網址\t公司名稱1\t公司類型1\t統一編號1\t部門1\t職稱1\t類別1\t公司地址1\t大樓名稱1\t公司寄發刊物1\t公司網址1\t公司名稱2\t公司類型2\t統一編號2\t部門2\t職稱2\t類別2\t公司地址2\t大樓名稱2\t公司寄發刊物2\t公司網址2\t接待人\t指派主管\t原經紀人\t經紀人\t建檔時間\t\n");
$i=1;
foreach($result as $ccc => $val){
	echo $i."\t";
	if($val->name!=""){
		if(!is_numeric($val->name)){
			echo @iconv("UTF-8","Big-5//ignore",$val->name)."\t";
		}else{
			//$str_num= settype($val->name , "String"); 
			echo @iconv("UTF-8","Big-5//ignore","'". $val->name."'")."\t";
		}
	}
	else{
		echo "\t";
	}
	if($val->recommended!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->recommended)."\t";
	else
		echo "\t";
	if(isset($importance_of_customers[$val->importance])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $importance_of_customers[$val->importance])."\t";
	else
		echo "\t";
	if($val->nationality)
		echo @iconv("UTF-8","Big-5//ignore","'". $nationality[$val->nationality])."\t";
	else 
		echo "\t";
	if($val->english_name!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->english_name)."\t";
	else 
		echo "\t";

	if($val->sex=="1")
		echo @iconv("UTF-8","Big-5//ignore","男")."\t";
	else
		echo @iconv("UTF-8","Big-5//ignore","女")."\t";
	
	if(isset($customer_source[$val->customer_source])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $customer_source[$val->customer_source])."\t";
	else
		echo "\t";
	if($val->birthday!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->birthday)."\t";
	else
		echo "\t";
	if($val->gifts!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->gifts)."\t";
	else
		echo "\t";
	if($val->id_number!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->id_number)."\t";
	else
		echo "\t";
	if(isset($contact_usual_holiday[$val->usually_contact]))
		echo @iconv("UTF-8","Big-5//ignore","'". $contact_usual_holiday[$val->usually_contact])."\t";
	else
		echo "\t";
	if($val->usually_start_time!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->usually_start_time)."\t";
	else
		echo "\t";
	if($val->usually_end_time!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->usually_end_time)."\t";
	else
		echo "\t";
	if(isset($contact_usual_holiday[$val->usually_contact1]))
		echo @iconv("UTF-8","Big-5//ignore","'". $contact_usual_holiday[$val->usually_contact1])."\t";
	else
		echo "\t";
	if($val->usually_start_time1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->usually_start_time1)."\t";
	else
		echo "\t";
	if($val->usually_end_time1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->usually_end_time1)."\t";
	else
		echo "\t";
	if(isset($contact_usual_holiday[$val->usually_contact2]))
		echo @iconv("UTF-8","Big-5//ignore","'". $contact_usual_holiday[$val->usually_contact2])."\t";
	else
		echo "\t";
	if($val->usually_start_time2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->usually_start_time2)."\t";
	else
		echo "\t";
	if($val->usually_end_time2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->usually_end_time2)."\t";
	else
		echo "\t";
	if(isset($contact_usual_holiday[$val->holidays_contact]))
		echo @iconv("UTF-8","Big-5//ignore","'". $contact_usual_holiday[$val->holidays_contact])."\t";
	else
		echo "\t";
	if($val->holidays_start_time!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->holidays_start_time)."\t";
	else
		echo "\t";
	if($val->holidays_end_time!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->holidays_end_time)."\t";
	else
		echo "\t";
	if(isset($contact_usual_holiday[$val->holidays_contact1]))
		echo @iconv("UTF-8","Big-5//ignore","'". $contact_usual_holiday[$val->holidays_contact1])."\t";
	else
		echo "\t";
	if($val->holidays_start_time1!="")		
		echo @iconv("UTF-8","Big-5//ignore","'". $val->holidays_start_time1)."\t";
	else
		echo "\t";
	if($val->holidays_end_time1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->holidays_end_time1)."\t";
	else
		echo "\t";
	if(isset($contact_usual_holiday[$val->holidays_contact2]))
		echo @iconv("UTF-8","Big-5//ignore","'". $contact_usual_holiday[$val->holidays_contact2])."\t";
	else
		echo "\t";
	if($val->holidays_start_time2!="")
		echo  @iconv("UTF-8","Big-5//ignore","'". $val->holidays_start_time2)."\t";
	else
		echo "\t";
	if($val->holidays_end_time2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->holidays_end_time2)."\t";
	else
		echo "\t";
	if($val->background_and_features!="")
		echo @iconv("UTF-8","Big-5//ignore",str_replace("\n", "'", $val->background_and_features))."\t";
	else
		echo "\t";
	if($val->mobile!="")
		echo @iconv("UTF-8","Big-5//ignore","'".$val->mobile )."\t";
	else
		echo "\t";
	if($val->send_sms!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->send_sms)."\t";
	else
		echo "\t";
	if($val->mobile1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->mobile1)."\t";
	else
		echo "\t";
	if($val->send_sms1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->send_sms1)."\t";
	else
		echo "\t";
	if($val->mobile2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->mobile2)."\t";
	else
		echo "\t";
	if($val->send_sms2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->send_sms2)."\t";
	else
		echo "\t";
	if($val->home_phone!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->home_phone)."\t";
	else
		echo "\t";
	if($val->home_phone1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->home_phone1)."\t";
	else
		echo "\t";
	if($val->home_phone2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->home_phone2)."\t";
	else
		echo "\t";
	if($val->company_phone!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_phone)."\t";
	else
		echo "\t";
	if($val->company_phone1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_phone1)."\t";
	else
		echo "\t";
	if($val->company_phone2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_phone2)."\t";
	else
		echo "\t";
	if($val->company_phone_ext!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_phone_ext)."\t";
	else
		echo "\t";
	if($val->company_phone_ext1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_phone_ext1)."\t";
	else
		echo "\t";
	if($val->company_phone_ext2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_phone_ext2)."\t";
	else
		echo "\t";
	if($val->email!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->email)."\t";
	else
		echo "\t";
	if($val->send_email!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->send_email)."\t";
	else
		echo "\t";
	if($val->email1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->email1)."\t";
	else
		echo "\t";
	if($val->send_email1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->send_email1)."\t";
	else
		echo "\t";
	if($val->email2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->email2)."\t";
	else
		echo "\t";
	if($val->send_email2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->send_email2)."\t";
	else
		echo "\t";
	if($val->fax!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->fax)."\t";
	else
		echo "\t";
	if($val->fax1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->fax1)."\t";
	else
		echo "\t";
	if($val->fax2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->fax2)."\t";
	else
		echo "\t";
	if($val->home_address!=""){
		echo @iconv("UTF-8","Big-5//ignore","'". $val->home_address)."\t";
		//echo @iconv("UTF-8","Big-5//ignore",$val->home_address)."\t";
	}else 
		echo "\t";
	if(isset($building_name[$val->home_building_name])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $building_name[$val->home_building_name])."\t";
	else
		echo "\t";
	if($val->home_send_publication!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->home_send_publication)."\t";
	else
		echo "\t";
	if($val->home_address1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->home_address1)."\t";
	else
		echo "\t";
	if(isset($building_name[$val->home_building_name1])) 		
		echo @iconv("UTF-8","Big-5//ignore","'". $building_name[$val->home_building_name1])."\t";
	else
		echo "\t";
	if($val->home_send_publication1!="") 
		echo @iconv("UTF-8","Big-5//ignore","'". $val->home_send_publication1)."\t";
	else
		echo "\t";
	if($val->home_address2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->home_address2)."\t";
	else
		echo "\t";
	if(isset($building_name[$val->home_building_name2])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $building_name[$val->home_building_name2])."\t";
	else
		echo "\t";
	if($val->home_send_publication2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->home_send_publication2)."\t";
	else
		echo "\t";
	if($val->company_name!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_name)."\t";
	else
		echo "\t";
	if(isset($type_of_company[$val->type_of_company])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $type_of_company[$val->type_of_company])."\t";
	else
		echo "\t";
	if($val->unicode!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->unicode)."\t";
	else
		echo "\t";
	if($val->department!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->department)."\t";
	else
		echo "\t";
	if(isset($title[$val->title])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $title[$val->title])."\t";
	else
		echo "\t";
	if(isset($customer_type[$val->category])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $customer_type[$val->category])."\t";
	else
		echo "\t";
	if($val->company_address!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_address)."\t";
	else
		echo "\t";
	if(isset($building_name[$val->company_building_name])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $building_name[$val->company_building_name])."\t";
	else
		echo "\t";
	if($val->company_send_publication!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_send_publication)."\t";
	else
		echo "\t";
	if($val->company_website!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_website)."\t";
	else
		echo "\t";
	if($val->company_name1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_name1)."\t";
	else
		echo "\t";
	if(isset($type_of_company[$val->type_of_company1])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $type_of_company[$val->type_of_company1])."\t";
	else
		echo "\t";
	if($val->unicode1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->unicode1)."\t";
	else
		echo "\t";
	if($val->department1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->department1)."\t";
	else
		echo "\t";
	if(isset($title[$val->title1])) 
		echo @iconv("UTF-8","Big-5//ignore","'". "'". $title[$val->title1])."\t";
	else
		echo "\t";
	if(isset($customer_type[$val->category1])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $customer_type[$val->category1])."\t";
	else
		echo "\t";
	if($val->company_address1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_address1)."\t";
	else
		echo "\t";	
	if(isset($building_name[$val->company_building_name1])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $building_name[$val->company_building_name1])."\t";
	else
		echo "\t";
	if($val->company_send_publication1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_send_publication1)."\t";
	else
		echo "\t";
	if($val->company_website1!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_website1)."\t";
	else
		echo "\t";
	if($val->company_name2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_name2)."\t";
	else
		echo "\t";
	if(isset($type_of_company[$val->type_of_company2])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $type_of_company[$val->type_of_company2])."\t";
	else
		echo "\t";
	if($val->unicode2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->unicode2)."\t";
	else
		echo "\t";
	if($val->department2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->department2)."\t";
	else
		echo "\t";
	if(isset($title[$val->title2])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $title[$val->title2])."\t";
	else
		echo "\t";
	if(isset($customer_type[$val->category2])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $customer_type[$val->category2])."\t";
	else
		echo "\t";
	if($val->company_address2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_address2)."\t";
	else
		echo "\t";
	if(isset($building_name[$val->company_building_name2])) 
		echo @iconv("UTF-8","Big-5//ignore","'". $building_name[$val->company_building_name2])."\t";
	else
		echo "\t";
	if($val->company_send_publication2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_send_publication2)."\t";
	else
		echo "\t";
	if($val->company_website2!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->company_website2)."\t";
	else
		echo "\t";
	if($val->front_man!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->front_man)."\t";
	else
		echo "\t";
	if($val->head!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->head)."\t";
	else
		echo "\t";
	if($val->original_broker!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->original_broker)."\t";
	else
		echo "\t";
	if($val->broker!="")
		echo @iconv("UTF-8","Big-5//ignore","'". $val->broker)."\t";
	else
		echo "\t";
	echo @iconv("UTF-8","Big-5//ignore",date("Y/m/d",$val->ctime))."\t\n";
	$i++;
}
?>